<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>取消请求</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>
</head>
<body>
    <button>发送请求</button>
    <button>取消请求</button>

    <script>

        let btns = document.querySelectorAll('button')

        let cancel


        btns[0].onclick = async function(){
            axios.post('http://localhost:3015/delay-server/123',null,{
                cancelToken:new axios.CancelToken((c)=>{
                    //保存取消请求的函数
                    cancel = c
                })
            })
        }

        btns[1].onclick = function(){
            //调用取消函数来取消请求
            cancel && cancel()
        }


        setTimeout(() => {
            
        }, timeout);












    </script>
</body>
</html>